//
//  DUVideoViewController.m
//  toutiao
//
//  Created by duyujin1 on 2021/7/21.
//

#import "DUVideoViewController.h"
#import "DUVideoCell.h"
#import "DUPlayerController.h"

@interface DUVideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation DUVideoViewController

-(instancetype) init{
    if(self=[super init]){
        NSString * title=NSLocalizedString(@"video", nil);
        self.tabBarItem.title=title;
        self.tabBarItem.image=[UIImage imageNamed:@"icon.bundle/video@2x.png"];
        self.tabBarItem.selectedImage=[UIImage imageNamed:@"icon.bundle/video_selected@2x.png"];
        self.navigationItem.title=title;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing=15;
    layout.minimumInteritemSpacing=20;
    layout.itemSize=CGSizeMake(self.view.bounds.size.width-30, (self.view.bounds.size.width-30)/16*9+DUVideoToolbarHeight);
    UICollectionView * collectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate=self;
    collectionView.dataSource=self;
    collectionView.backgroundColor=[UIColor whiteColor];
    [collectionView registerClass:[DUVideoCell class] forCellWithReuseIdentifier:@"DUVideoCell"];
    [self.view addSubview: collectionView];
}


#pragma mark ---collectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DUVideoCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"DUVideoCell"
                                                                          forIndexPath:indexPath];
    if([cell isKindOfClass:[DUVideoCell class]]){
        [(DUVideoCell *)cell layoutWithVideoCoverUrl:@"videoCover" videoUrl:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
    }
    return cell;
}

#pragma mark-

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DUPlayerController * videoController=[[DUPlayerController alloc] init];
    videoController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:videoController animated:YES];
}

@end
